
package com.tools.cleanmaster.hijack;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.text.TextUtils;

/**
 * @author yuhenghui@baidu.com
 */
class ProcessUtils {

    private static String sMyProcessName = null;

    static String myProcess(Context context) {
        if (TextUtils.isEmpty(sMyProcessName)) {
            int myPid = android.os.Process.myPid();
            sMyProcessName = getProcessName(context, myPid);
        }
        return sMyProcessName;
    }

    private static String getProcessName(Context ctx, int pid) {
        ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return "unknown";
    }

}
